package com.cw.frame.codegen.parameter;

import org.apache.commons.lang3.StringUtils;

import com.cw.jeeyt.common.enums.EnumDataJavaType;
import com.cw.jeeyt.common.enums.EnumMysqlDataType;

/**
 * To support the parameter transformation.
 *
 * @author chenw
 */
public class ParamHelper {

    /**
     * Convert column name, such as 'user_name' to 'userName'
     *
     * @param columnName
     * @return
     */
    public static String getFieldName(String columnName) {
        if (StringUtils.isEmpty(columnName)) {
            return null;
        }
        if (columnName.endsWith("_")) {
            columnName = columnName.substring(0, columnName.length() - 1) + "--";
        }
        columnName = columnName.toLowerCase();
        String[] names = columnName.split("_");
        StringBuilder filedName = new StringBuilder();
        for (int i = 0; i < names.length; i++) {
            if (names[i].trim()
                .equals("")) {
                continue;
            }
            if (filedName.length() == 0) {
                filedName.append(names[i].trim());
                continue;
            }
            filedName.append(StringUtils.capitalize(names[i].trim()));
        }
        if (filedName.toString()
            .contains("--")) {
            return filedName.toString()
                .replace("--", "_");
        } else {
            return filedName.toString();
        }

    }

    /**
     * Convert table name, such as 'system_user' to 'SystemUser'
     *
     * @param tableCode
     * @return
     */
    public static String getTableEntityCode(String tableCode) {
        if (StringUtils.isEmpty(tableCode)) {
            return null;
        }
        tableCode = tableCode.toLowerCase();
        String[] names = tableCode.split("_");
        StringBuilder filedName = new StringBuilder();
        for (int i = 0; i < names.length; i++) {
            if (names[i].trim()
                .equals("")) {
                continue;
            }
            filedName.append(StringUtils.capitalize(names[i].trim()));
        }

        return filedName.toString();
    }

    /**
     * Convert field name, such as 'userName' to 'user_name'
     *
     * @param fieldName
     * @return
     */
    public static String getColumnName(String fieldName) {
        if (fieldName == null) {
            return null;
        }
        StringBuilder columnName = new StringBuilder();
        for (int i = 0; i < fieldName.length(); i++) {
            char ch = fieldName.charAt(i);
            if (Character.isUpperCase(ch) && columnName.length() > 0) {
                columnName.append("_");
            }
            columnName.append(ch);
        }
        return columnName.toString()
            .toLowerCase();
    }

    public static String sqlType2JavaType(String sqlType) {
        if (sqlType.equalsIgnoreCase("bit")) {
            return "Boolean";
        } else if (sqlType.equalsIgnoreCase("int") || sqlType.equalsIgnoreCase("smallint") || sqlType.equalsIgnoreCase(
            "tinyint")) {
            return "Integer";
        } else if (sqlType.equalsIgnoreCase("bigint")) {
            return "Long";
        } else if (sqlType.equalsIgnoreCase("float")) {
            return "Float";
        } else if (sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric")
            || sqlType.equalsIgnoreCase("real")) {
            return "Double";
        } else if (sqlType.equalsIgnoreCase("money") || sqlType.equalsIgnoreCase("smallmoney")) {
            return "Double";
        } else if (sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase(
            "nvarchar") || sqlType.equalsIgnoreCase("nchar")) {
            return "String";
        } else if (sqlType.equalsIgnoreCase("datetime") || sqlType.equalsIgnoreCase("timestamp")
            || sqlType.equalsIgnoreCase("date")) {
            return "java.util.Date";
        } else if (sqlType.equalsIgnoreCase("image")) {
            return "java.sql.Blob";
        } else if (sqlType.equalsIgnoreCase("text")) {
            return "String";
        }
        return "String";
    }

    public static String sqlType2JavaType(String sqlType, int scale) {
        if (sqlType.equalsIgnoreCase("bit")) {
            return "Boolean";
        } else if (sqlType.equalsIgnoreCase("int") || sqlType.equalsIgnoreCase("smallint") || sqlType.equalsIgnoreCase(
            "tinyint") || (sqlType.equalsIgnoreCase("number") && scale == 0)) {
            return "Integer";
        } else if (sqlType.equalsIgnoreCase("bigint")) {
            return "Long";
        } else if (sqlType.equalsIgnoreCase("float")) {
            return "Float";
        } else if (sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric")
            || sqlType.equalsIgnoreCase("real") || (sqlType.equalsIgnoreCase("number") && scale > 0)) {
            return "Double";
        } else if (sqlType.equalsIgnoreCase("money") || sqlType.equalsIgnoreCase("smallmoney")) {
            return "Double";
        } else if (sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase(
            "nvarchar") || sqlType.equalsIgnoreCase("nchar") || sqlType.equalsIgnoreCase("nvarchar2")) {
            return "String";
        } else if (sqlType.equalsIgnoreCase("datetime") || sqlType.equalsIgnoreCase("timestamp")
            || sqlType.equalsIgnoreCase("date")) {
            return "java.util.Date";
        } else if (sqlType.equalsIgnoreCase("image")) {
            return "java.sql.Blob";
        } else if (sqlType.equalsIgnoreCase("text")) {
            return "String";
        }
        return "String";
    }

    public static String convertSqlTypeToJavaType(String sqlType) {
        if (sqlType == null || "".equals(sqlType)) {
            return null;
        }
        String javaType = "";
        if (sqlType.equals(EnumMysqlDataType.CHAR.getCode()) || sqlType.equals(EnumMysqlDataType.VARCHAR.getCode())
            || sqlType.equals(EnumMysqlDataType.TINYTEXT.getCode()) || sqlType.equals(EnumMysqlDataType.TEXT.getCode())
            || sqlType.equals(EnumMysqlDataType.BLOB.getCode()) || sqlType.equals(
            EnumMysqlDataType.MEDIUMTEXT.getCode()) || sqlType.equals(EnumMysqlDataType.MEDIUMBLOB.getCode())
            || sqlType.equals(EnumMysqlDataType.LONGTEXT.getCode()) || sqlType.equals(
            EnumMysqlDataType.LONGBLOB.getCode())) {
            javaType = EnumDataJavaType.STRING.getCode();
        } else if (sqlType.equals(EnumMysqlDataType.INT.getCode()) || sqlType.equals(
            EnumMysqlDataType.INTEGER.getCode()) || sqlType.equals(EnumMysqlDataType.TINYINT.getCode())) {
            javaType = EnumDataJavaType.INTEGER.getCode();
        } else if (sqlType.equals(EnumMysqlDataType.SMALLINT.getCode()) || sqlType.equals(
            EnumMysqlDataType.MEDIUMINT.getCode()) || sqlType.equals(EnumMysqlDataType.BIGINT.getCode())) {
            javaType = EnumDataJavaType.LONG.getCode();
        } else if (sqlType.equals(EnumMysqlDataType.FLOAT.getCode())) {
            javaType = EnumDataJavaType.FLOAT.getCode();
        } else if (sqlType.equals(EnumMysqlDataType.DOUBLE.getCode())) {
            javaType = EnumDataJavaType.DOUBLE.getCode();
        } else if (sqlType.equals(EnumMysqlDataType.DECIMAL.getCode())) {
            javaType = EnumDataJavaType.BIGDECIMAL.getCode();
        } else if (sqlType.equals(EnumMysqlDataType.TIMESTAMP.getCode()) || sqlType.equals(
            EnumMysqlDataType.TIME.getCode()) || sqlType.equals(EnumMysqlDataType.DATETIME.getCode()) || sqlType.equals(
            EnumMysqlDataType.DATE.getCode())) {
            javaType = EnumDataJavaType.DATE.getCode();
        }
        return javaType;
    }
}
